/**
 * BeanUtilsTests
 *
 * @author Chris Pratt
 *
 * 4/13/2010
 */
package com.anodyzed.onyx.bean;

import com.anodyzed.onyx.log.LogBuilder;
import com.anodyzed.onyx.log.sysout.SysOutLogFactory;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;

public class BeanUtilsTests {
  public static class TestData {
    class Inner {
      public String getText () {
        return "inner";
      } //getText
    } //*Inner

    public String getText () {
      return "outer";
    } //getText

    public Object getInner () {
      return new Inner();
    } //getInner

    public Date getDate () {
      return new GregorianCalendar(1965,Calendar.JUNE,18).getTime();
    } //getDate

    public int getNum () {
      return 42;
    } //getNum

    public String getStr () {
      return "Test";
    } //getStr

    /**
     * @param str unused 
     */
    public void setStr (String str) {
    } //setStr

  } //*TestData

  /**
   * Setup the Test Harness
   */
  @BeforeClass
  public static void setup () {
    LogBuilder.setLogFactory(new SysOutLogFactory());
  } //setup

  /**
   * Perform a jUnit Test on the BeanUtils getDescriptor capability
   */
  @Test public void testDescriptor () throws IntrospectionException {
    PropertyDescriptor desc = BeanUtils.getDescriptor(TestData.class,"date");
    assertNotNull("BeanUtils.getDescriptor(TestData.class,\"date\") returned null",desc);
    assertNotNull("PropertyDescriptor.getReadMethod() returned null",desc.getReadMethod());
  } //testDescriptor

  /**
   * Perform a jUnit Test on the BeanUtils getAccessor capability
   */
  @Test public void testAccessor () throws IntrospectionException {
    assertNotNull("BeanUtils.getAccessor(TestData.class,\"date\") returned null",BeanUtils.getAccessor(TestData.class,"date"));
  } //testAccessor

  /**
   * Perform a jUnit Test on the BeanUtils getAccessors capability
   */
  @Test public void testAccessors () throws IntrospectionException {
    List<Method> accessors = BeanUtils.getAccessors(TestData.class);
    assertEquals("BeanUtils.getAccessors(TestData.class) return the wrong number of accessors",6,accessors.size());
  } //testAccessors

  /**
   * Perform a jUnit Test on the BeanUtils getMutator capability
   */
  @Test public void testMutator () throws IntrospectionException {
    assertNotNull("BeanUtils.getMutator(TestData.class,\"str\") returned null",BeanUtils.getMutator(TestData.class,"str"));
  } //testMutator

  /**
   * Perform a jUnit Test on the BeanUtils getPropertyType capability
   */
  @Test public void testPropertyType () throws IntrospectionException {
    Class<?> cls = BeanUtils.getPropertyType(TestData.class,"date");
    assertNotNull("BeanUtils.getPropertyType(TestData.class,\"date\") returned null",cls);
    assertEquals("BeanUtils.getPropertyType(TestData.class,\"date\") doesn't equal java.util.Date",Date.class,cls);
  } //testPropertyType

  /**
   * Perform a jUnit Test on the BeanUtils getValue capability
   */
  @Test public void testValue () throws IntrospectionException {
    TestData data = new TestData();
    assertEquals("BeanUtils.getValue(data,\"text\") != \"outer\"","outer",BeanUtils.getValue(data,"text"));
    assertEquals("BeanUtils.getValue(data,\"num\") != 42",42,BeanUtils.getValue(data,"num"));
    assertEquals("BeanUtils.getValue(data,\"str\") != \"Test\"","Test",BeanUtils.getValue(data,"str"));
    assertEquals("BeanUtils.getValue(data,\"inner.text\") != \"inner\"","inner",BeanUtils.getValue(data,"inner.text"));
  } //testValue

  /**
   * Perform a jUnit Test on the BeanUtils getString capability
   */
  @Test public void testString () {
    TestData data = new TestData();
    assertEquals("BeanUtils.getString(data,\"text\") != \"outer\"","outer",BeanUtils.getString(data,"text"));
    assertEquals("BeanUtils.getString(data,\"num\") != 42","42",BeanUtils.getString(data,"num"));
    assertEquals("BeanUtils.getString(data,\"str\") != \"Test\"","Test",BeanUtils.getString(data,"str"));
    assertEquals("BeanUtils.getString(data,\"inner.text\") != \"inner\"","inner",BeanUtils.getString(data,"inner.text"));
  } //testString

  /**
   * jUnit 3.x Test Suite
   *
   * @return Adapted Test Suite
   */
  public static junit.framework.Test suite () {
    return new junit.framework.JUnit4TestAdapter(BeanUtilsTests.class);
  } //suite

} //*BeanUtilsTests
